{$CLEO .cs}
{$USE CLEO+}
{$USE newOpcodes}
{$USE ini}

0000: NOP

script_name "Lamppost Fx"

// By ArtemQa146

if
    0AA2: 31@ = load_dynamic_library "CLEO+.cleo"
then
    if
        0AA4: 30@ = get_dynamic_library_procedure "GetCleoPlusVersion" library 31@
    then
        0AA7: call_function_return 30@ num_params 0 pop 0 29@
        if
            not 29@ >= 0x1020000
        then
            0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the Lamppost FX mod is disabled" time 15000
            0AA3: free_dynamic_library 31@
            0A93: terminate_this_custom_script
        end
        0AA3: free_dynamic_library 31@
    end
else
    0AD1: show_formatted_text_highpriority "Cleo+ 1.2.0 (or higher) version is required, the Lamppost FX mod is disabled" time 15000
    0AA3: free_dynamic_library 31@
    0A93: terminate_this_custom_script
end

const
    find = 31@
    lampost = 30@
    lampost_fx = 29@
    Check_Model = 28@
    find_fx = 27@
    lampost_model = 26@
    test_fx = 25@ 
    more_lamp = 24@
    distance_z = 23@
    lampost_model_white = 22@
    color_lamp = 21@
    next = 20@
    yellow = 1
    white = 2
end

0AF2: distance_z = read_float_from_ini_file "cleo\Lamppost FX.ini" section "Settings" key "Distance"
0AF0: lampost_model = read_int_from_ini_file "cleo\Lamppost FX.ini" section "Settings" key "Model ID"
0AF0: lampost_model_white = read_int_from_ini_file "cleo\Lamppost FX.ini" section "Settings" key "Model ID 2"
find = 0

while true
wait 0
    if
        player.Defined(0)
    then
    
    //0AD1: show_formatted_text_highpriority "%d" time 2000 find
    if
        003B:   next == find  // (int)
    then
        find = 0
    end

        0085: next = find // (int)
        while get_any_object_no_save_recursive find progress_to find object_to lampost
        
        gosub @Model
            if
                Check_Model == true
            then
                0A98: 1@ = object lampost struct
                0D4E: 2@ = read_struct 1@ offset 0x140 size 4
                if and
                    88B7:   not test 2@ bit 10 // The object is broken
                    88B7:   not test 2@ bit 6 // The object is expl
                    0EE6: locate_char_distance_to_object $player_actor object lampost radius distance_z
                then
                    test_fx = 146
                    break
                    
                else
                    0A98: 1@ = object lampost struct
                    0D4E: 1@ = read_struct 1@ offset 0x140 size 4
                    if or
                        08B7:   test 2@ bit 6 // The object is expl
                        08B7:   test 1@ bit 10 // The object is broken
                    then
                        test_fx = 228
                        break
                    end
                end
            end
            
            
            if or
                09CC:   object lampost model_is lampost_model
                09CC:   object lampost model_is lampost_model_white
            then
                0087: 1@ = distance_z // (float)
                1@ += 20.0
                if
                    not  locate_char_distance_to_object $player_actor object lampost radius 1@
                then
                    01C4: remove_references_to_object lampost // This object will now disappear when the player looks away
                    break
                end
            end
        end
        
        if
            test_fx == 146
        then
            wait 0
            test_fx = false
            
            find_fx = 0
            while get_any_object_no_save_recursive find_fx progress_to find_fx object_to lampost_fx
                if
                    03CA:   object lampost exists
                then
                    if or
                        09CC:   object lampost_fx model_is lampost_model
                        09CC:   object lampost_fx model_is lampost_model_white
                    then
                        gosub @Offset
                        0400: store_coords_to 1@ 2@ 3@ from_object lampost with_offset 10@ 11@ 12@
                        if
                            0EEC: locate_object_distance_to_coordinates lampost_fx pos 1@ 2@ 3@ radius 5.0
                        then
                            test_fx = true
                            break
                        end
                    end
                else
                    break
                end
            end


            if and
                03CA:   object lampost exists
                test_fx == false
            then
                gosub @Offset
                // 4@ - lamposts model
                if color_lamp == yellow
                then 0085: 4@ = lampost_model // (int)
                else 0085: 4@ = lampost_model_white // (int)
                end
                model.Load(4@)
                038B: load_requested_models
                0400: store_coords_to 1@ 2@ 3@ from_object lampost with_offset 10@ 11@ 12@
                0E01: create_object_no_save 4@ at 1@ 2@ 3@ offset 0 ground 0 to lampost_fx
                if more_lamp == 2
                then
                    0400: store_coords_to 1@ 2@ 3@ from_object lampost with_offset 13@ 14@ 15@
                    0E01: create_object_no_save 4@ at 1@ 2@ 3@ offset 0 ground 0 to lampost_fx
                end
                if more_lamp == 3
                then
                    0400: store_coords_to 1@ 2@ 3@ from_object lampost with_offset 13@ 14@ 15@
                    0E01: create_object_no_save 4@ at 1@ 2@ 3@ offset 0 ground 0 to lampost_fx
                    0400: store_coords_to 1@ 2@ 3@ from_object lampost with_offset 16@ 17@ 18@
                    0E01: create_object_no_save 4@ at 1@ 2@ 3@ offset 0 ground 0 to lampost_fx
                end
                model.Destroy(4@)
            end
        end
        
        if
            test_fx == 228
        then
            wait 0
            test_fx = false
            
            find_fx = 0
            while get_any_object_no_save_recursive find_fx progress_to find_fx object_to lampost_fx
                if
                    03CA:   object lampost exists
                then
                    if or
                        09CC:   object lampost_fx model_is lampost_model
                        09CC:   object lampost_fx model_is lampost_model_white
                    then
                        gosub @Offset
                        0400: store_coords_to 1@ 2@ 3@ from_object lampost with_offset 10@ 11@ 12@
                        if
                            0EEC: locate_object_distance_to_coordinates lampost_fx pos 1@ 2@ 3@ radius 12.0
                        then
                            0108: destroy_object lampost_fx
                        end
                    end
                else
                    break
                end
            end
        end
        
        
    end
end


    // Model verification
:Model
Check_Model = false

// Traffic lights IDs
if or
    09CC:   object lampost model_is 3855 // GAY_TRAFFIC_LIGHT
    09CC:   object lampost model_is 1350 // CJ_TRAFFIC_LIGHT4
    09CC:   object lampost model_is 1351 // CJ_TRAFFIC_LIGHT5
then
    Check_Model = true
    return
end

// Lamp posts IDs
if or
    09CC:   object lampost model_is 1290 // lamppost2
    09CC:   object lampost model_is 1226 // lamppost3
    09CC:   object lampost model_is 3460 // vegaslampost
    09CC:   object lampost model_is 1568 // chinalamp_sf
    09CC:   object lampost model_is 3463 // vegaslampost2
then
    Check_Model = true
    return
end
if or
    09CC:   object lampost model_is 3472 // circuslampost03
    09CC:   object lampost model_is 1294 // mlamppost
    09CC:   object lampost model_is 1297 // lamppost1
    09CC:   object lampost model_is 1232 // Streetlamp1
    09CC:   object lampost model_is 1231 // Streetlamp2
    09CC:   object lampost model_is 3853 // Gay_lamppost
    09CC:   object lampost model_is 1223 // lampost_coast
then
    Check_Model = true
    return
end
return


    // Light source location offset
:Offset
more_lamp = 1
color_lamp = yellow

// Traffic lights ------------------------------------------------------
if  or
    09CC:   object lampost model_is 3855 // GAY_TRAFFIC_LIGHT
    09CC:   object lampost model_is 1350 // CJ_TRAFFIC_LIGHT4
then
    10@ = -2.046
    11@ = 0.119
    12@ = 7.591
    return
end
if
    09CC:   object lampost model_is 1351 // CJ_TRAFFIC_LIGHT5
then
    10@ = -1.69
    11@ = 0.017
    12@ = 6.007
    return
end

// Lamp posts ------------------------------------------------------
if
    09CC:   object lampost model_is 1226 // lamppost3
then
    10@ = -1.402684
    11@ = 0.212558
    12@ = 3.670264
    return
end
if
    09CC:   object lampost model_is 1223 // lampost_coast
then
    10@ = 0.645016
    11@ = -0.00069
    12@ = 4.420537
    color_lamp = white
    return
end
if
    09CC:   object lampost model_is 1297 // lamppost1
then
    10@ = -0.435251
    11@ = 0.048412
    12@ = 3.202
    return
end
if
    09CC:   object lampost model_is 1290 // lamppost2
then
    10@ = 2.983543
    11@ = 0.010445
    12@ = 5.320805

    13@ = -2.988853
    14@ = 0.010445
    15@ = 5.322746

    more_lamp = 2
    return
end
if
    09CC:   object lampost model_is 3472 // circuslampost03
then
    10@ = -1.680908
    11@ = -2.93335
    12@ = 9.96314

    13@ = -1.717773
    14@ = 2.884766
    15@ = 9.96314

    16@ = 3.28833
    17@ = -0.012695
    18@ = 9.96314

    more_lamp = 3
    return
end
if
    09CC:   object lampost model_is 1568 // chinalamp_sf
then
    10@ = -0.019892
    11@ = 0.005714
    12@ = 3.628155
    return
end
if
    09CC:   object lampost model_is 3853 // Gay_lamppost
then
    10@ = -1.300914
    11@ = 0.171993
    12@ = 3.601248
    return
end
if
    09CC:   object lampost model_is 1294 // mlamppost
then
    10@ = -1.407333
    11@ = 0.001591
    12@ = 4.100351
    return
end
if
    09CC:   object lampost model_is 1232 // Streetlamp1
then
    10@ = 0.036
    11@ = 0.0
    12@ = 2.718
    color_lamp = white
    return
end
if
    09CC:   object lampost model_is 1231 // Streetlamp2
then
    10@ = 0.536
    11@ = -0.003
    12@ = 2.640

    13@ = -0.465
    14@ = -0.003
    15@ = 2.640

    more_lamp = 2
    color_lamp = white
    return
end
if
    09CC:   object lampost model_is 3460 // vegaslampost
then
    10@ = 0.007446
    11@ = 1.548096
    12@ = 3.58964
    return
end
if
    09CC:   object lampost model_is 3463 // vegaslampost2
then
    10@ = 7.402832
    11@ = 0.00415
    12@ = 11.107159

    13@ = -7.376953
    14@ = 0.00415
    15@ = 11.107159

    more_lamp = 2
    return
end
return